﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApp.Template.Attributes;
using RuoVea.OmiApp.Template.Dto;
using RuoVea.ExDto;
using RuoVea.OmiApp.Template.Service;

namespace RuoVea.OmiApp.Template.Controllers;

/// <summary>
/// 
/// </summary>
[AppRoute]
public class DictValueController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly ISysDictValueService _sysDictValueService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysDictValueService"></param>
    public DictValueController(ISysDictValueService sysDictValueService)
    {
        _sysDictValueService = sysDictValueService;
    }
    /// <summary>
    /// 参数配置页面
    /// </summary>
    [HttpGet("Index")]
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysDictValueDto>>> GetPagesAsync(SysDictValueParam data)
    {
        return await _sysDictValueService.GetPagesAsync(data);
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("data/{id:long}")]
    public async Task<RestfulResult<SysDictValueDto>> GetDataAsync(long id)
    {
        return await _sysDictValueService.GetDataAsync(id);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("submit")]
    public async Task<RestfulResult> SubmitAsync(SysDictValueDto data)
    {
        if (data.Id == 0)
        {
            return await _sysDictValueService.AddDataAsync(data);
        }
        else
        {
            return await _sysDictValueService.UpdateDataAsync(data);
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("delete/{id:long}")]
    public async Task<RestfulResult> DeleteDataAsync(long id)
    {
        return await _sysDictValueService.DeleteDataAsync(id);
    }


    /// <summary>
    /// 获取某个字典类型下字典值列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("getdictdatalistbycode")]
    [AllowAnonymous]
    public async Task<RestfulResult<List<Dictionary<string, string>>>> GetDictDataListByCode(QueryDictValueListByCodeInput data)
    {
        return await _sysDictValueService.GetDictDataListByCode(data);
    }

    /// <summary>
    /// 修改字典值状态
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("changedictdatastatus")]
    public async Task<RestfulResult> ChangeDictDataStatus(ChageStateDictValueInput data)
    {
        return await _sysDictValueService.ChangeDictDataStatus(data);
    }
}